<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SubsidiesInit extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cc:subsidy-init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
       User::query()
            ->where('gold_subsidies',">",0)
            //->whereHas('goldCharges')
            ->chunks(50, function (Collection $users) {

                if ($users->isEmpty()) return true;

                /**
                 * @var User $user
                 */
                foreach ($users as $user) {
                   $todayCoin = GoldCoin::query()->where("user_id",$user->id)->where("remark","gold_record_direct_prize")->whereDate("created_at",Carbon::today())->sum('money');
                   if(big_decimal($todayCoin)->isGreaterThan($user->gold_subsidies)){
                        account_subsidies($user)->minusUnlockedSubsidise($user->gold_subsidies);
                        account_coins($user)->plusUnlockedSubsidise($user->gold_subsidies);
                   }else{

                        account_subsidies($user)->minusUnlockedSubsidise($todayCoin);
                        account_coins($user)->plusUnlockedSubsidise($todayCoin);
                   }

                }

                return true;

            });
    }
}
